package com.wdata.system.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.wdata.base.service.CrudService;
import com.wdata.base.util.PageData;
import com.wdata.system.dao.MenuDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ClassName MenuService
 * <p>Description 菜单Service</p>
 *
 * @author Yat
 * Date 2024年1月31日, 0031 15:36
 * version 1.0
 */
@Service
@Transactional
public class MenuService extends CrudService<MenuDao, PageData> {

    @Autowired
    protected MenuDao menuDao;

    public void delForParam(PageData pd) {
        menuDao.delForParam(pd);
    }

    public List<PageData> findMenuList(PageData pd) {

        List<PageData> list = menuDao.findMenuList(pd);//查找一级菜单
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                list.get(i).put("spread", "true");
            }
            pd.put("type", "menu");
            pd.put("level", "2");
            pd.put("parent_id", list.get(i).get("id"));
            List<PageData> ls = menuDao.findMenuList(pd);//查询二级菜单
            if (ls.size() > 0) {
                for (int j = 0; j < ls.size(); j++) {
                    pd.put("type", "button");
                    pd.put("level", "3");
                    pd.put("parent_id", ls.get(j).get("id"));
                    List<PageData> buttonLs = menuDao.findMenuList(pd);
                    StringBuffer sb = new StringBuffer();
                    for (PageData buttonL : buttonLs) {
                        sb.append(buttonL.get("code")).append(",");
                    }

                    String path = pd.get("contextPath") + ls.get(j).get("href").toString();
                    if ( StrUtil.isNotBlank(path)) {
                        if (path.contains("?")) {
                            path = path + "&";
                        } else {
                            path = path + "?";
                        }
                        if (ObjectUtil.isNotNull(ls.get(j).get("user_auth_id"))) {
                            if (ls.get(j).get("user_auth_type").equals("5")) {
                                path = path + "auth_type=" + ls.get(j).get("user_auth_type") + "&auth_organize_ids=" + ls.get(j).get("user_auth_organize_ids");
                            } else {
                                path = path + "auth_type=" + ls.get(j).get("user_auth_type");
                            }
                        } else {
                            path = path + "auth_type=1";
                        }

                        if (sb.length() > 0) {
                            path = path + "&authParam=" + sb.substring(0, sb.length() - 1);
                            //                        ls.get(j).put("href",ls.get(j).get("href")+"?authParam="+sb
                            //                        .substring(0,sb.length()-1));
                        }
                        ls.get(j).put("href", path);
                    } else {
                        ls.get(j).put("href", "");
                    }
                }
                list.get(i).put("children", ls);
            }
            pd.put("type", "button");
            pd.put("level", "2");
            pd.put("parent_id", list.get(i).get("id"));
            List<PageData> butLs = menuDao.findMenuList(pd);
            StringBuffer buffer = new StringBuffer();
            for (int j = 0; j < butLs.size(); j++) {
                buffer.append(butLs.get(i).get("code")).append(",");
            }
            if (ObjectUtil.isNotNull(list.get(i).get("href"))) {
                String path = pd.get("contextPath") + list.get(i).get("href").toString();
                if ( StrUtil.isNotBlank(path)) {
                    if (path.contains("?")) {
                        path = path + "&";
                    } else {
                        path = path + "?";
                    }
                    if (ObjectUtil.isNotNull(list.get(i).get("user_auth_id"))) {
                        if (list.get(i).get("user_auth_type").equals("5")) {
                            path = path + "auth_type=" + list.get(i).get("user_auth_type") + "&auth_organize_ids=" + list.get(i).get("user_auth_organize_ids");
                        } else {
                            path = path + "auth_type=" + list.get(i).get("user_auth_type");
                        }
                    } else {
                        path = path + "auth_type=1";
                    }
                    if (buffer.length() > 0) {
                        path = path + "&authParam=" + buffer.substring(0, buffer.length() - 1);
                        //                list.get(i).put("href",list.get(i).get("href")+"?authParam="+butsb
                        //                .substring(0,butsb.length()-1));
                    }
                    list.get(i).put("href", path);
                } else {
                    list.get(i).put("href", "");
                }
            }
        }
        return list;
    }

    public List<PageData> findMList(PageData pd) {
        return menuDao.findMenuList(pd);
    }

    public List<PageData> findUserMenuList(PageData pd) {
        pd.put("type", "menu");
        pd.put("level", "0");
        pd.put("parent_id", "0");
        List<PageData> oneLs = menuDao.findMenuList(pd);//查找一级菜单
        for (int j = 0; j < oneLs.size(); j++) {
            pd.put("level", "1");
            pd.put("parent_id", oneLs.get(j).get("id"));
            List<PageData> list = menuDao.findMenuList(pd);//查找二级菜单
            for (int i = 0; i < list.size(); i++) {
                pd.put("type", "menu");
                pd.put("level", "2");
                pd.put("parent_id", list.get(i).get("id"));
                List<PageData> ls = menuDao.findMenuList(pd);//查询三级菜单

                list.get(i).put("children_children", ls);
            }
            oneLs.get(j).put("children", list);
        }
        return oneLs;
    }

    public void delRoleMenu(String id) {
        menuDao.delRoleMenu(id);
    }

}
